package com.android.lovely.ui.fragment

import androidx.core.view.isVisible
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.android.lovely.R
import com.android.lovely.const.CreatedRoleRefresh
import com.android.lovely.const.HomeShowRole
import com.android.lovely.const.HomeShowTask
import com.android.lovely.databinding.FragmentWorkRoleBinding
import com.android.lovely.framework.BaseFragment
import com.android.lovely.ui.dialog.CreateImageTypeDialog
import com.android.lovely.ui.dialog.CreateRoleTypeDialog
import com.android.lovely.ui.fragment.role.CreateRoleStep1Fragment
import com.android.lovely.ui.fragment.role.CreateRoleStep2Fragment
import com.android.lovely.ui.fragment.role.CreateRoleStep3Fragment
import com.android.lovely.ui.fragment.role.CreateRoleStep4Fragment
import com.android.lovely.ui.fragment.role.CreateRoleStep5Fragment
import com.android.lovely.ui.viewmodel.WorkCreateViewModel
import com.android.lovely.ui.viewmodel.WorkTaskViewModel
import com.google.android.play.integrity.internal.n
import com.kotlin.extensions.bus.FlowBus
import com.kotlin.extensions.bus.flowOf
import com.kotlin.extensions.coroutine.CoroutineExt.launchSafety
import com.kotlin.extensions.view.ViewExt.clickable
import kotlinx.coroutines.launch

/**
 *
 * @author why
 * @date 2025/4/8 16:03
 */
class WorkActivityFragment: BaseFragment<FragmentWorkRoleBinding>() {

    val viewModel by activityViewModels<WorkCreateViewModel>()
    val taskViewModel by activityViewModels<WorkTaskViewModel>()

    private val roleTabsType by lazy {
        arrayOf(getString(R.string.all), getString(R.string.in_review), getString(R.string.be_rejected), getString(R.string.passed))
    }

    private val roleTabs by lazy {
        arrayOf(getString(R.string.public_desc), getString(R.string.private_desc), getString(R.string.draft))
    }

    private val taskTabsType by lazy {
        arrayOf(getString(R.string.all), getString(R.string.passed), getString(R.string.checking), getString(R.string.failed))
    }

    var currentPosition = 0

    override fun initView() {
        mBinding.vp2.isUserInputEnabled = false
        mBinding.vp2.adapter = FragmentAdapter(childFragmentManager, lifecycle)
        mBinding.vp2.offscreenPageLimit = 1
        mBinding.vp2.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                selectPosition(position)
            }
        })
        val task = arguments?.getBoolean("task", false)
//        val select = if(task == true) 1 else 0
        mBinding.vp2.currentItem = 0
        mBinding.tvRole.isVisible = false
        mBinding.tvRole.clickable {
            mBinding.vp2.currentItem = 0
        }
        mBinding.tvTask.clickable {
            mBinding.vp2.currentItem = 0
        }
        mBinding.tvRoleSelect.clickable {
//            if(currentPosition == 0) {
//                CreateRoleTypeDialog().show(fragmentManager = childFragmentManager)
//            } else {
                CreateImageTypeDialog().show(fragmentManager = childFragmentManager)
//            }
        }
        lifecycleScope.launchSafety{
            viewModel.roleType.observe(this@WorkActivityFragment){
                mBinding.tvRoleSelect.text = taskTabsType[when(taskViewModel.status){
                    1 -> 1
                    0 -> 2
                    null -> 0
                    else -> 3
                }]
            }
        }
        lifecycleScope.launchSafety{
            taskViewModel.statueType.observe(this@WorkActivityFragment){
                mBinding.tvRoleSelect.text = taskTabsType[when(taskViewModel.status){
                    1 -> 1
                    0 -> 2
                    null -> 0
                    else -> 3
                }]
            }
        }
        viewLifecycleOwner.lifecycleScope.launch {
            FlowBus.flowOf<HomeShowRole>().collect {
                selectPosition(0)
                mBinding.vp2.currentItem = 0
            }
        }
        viewLifecycleOwner.lifecycleScope.launch {
            FlowBus.flowOf<HomeShowTask>().collect {
                selectPosition(1)
                mBinding.vp2.currentItem = 1
            }
        }
    }

    fun selectPosition(position: Int) {
        currentPosition = position
        mBinding.tvRole.isSelected = position == 0
        mBinding.tvTask.isSelected = position == 0
        mBinding.tvRoleSelect.text = taskTabsType[when(taskViewModel.status){
            1 -> 1
            0 -> 2
            null -> 0
            else -> 3
        }]
    }

    override suspend fun initData() {

    }

    private inner class FragmentAdapter(fragmentManager: FragmentManager, lifeCircle: Lifecycle) : FragmentStateAdapter(fragmentManager, lifeCircle) {

        override fun getItemCount(): Int {
            return 1
        }

        override fun createFragment(position: Int): Fragment {
            return WorkTaskFragment()
        }
    }
}